#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
#include <QAction>
#include <math.h>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog),
    counter(0)
{
    ui->setupUi(this);

    connect(this, SIGNAL(signalCalc()), this, SLOT(on_pushButtonCalc_clicked()));

    ui->lineEditArea->setEnabled(false);
    this->setContextMenuPolicy(Qt::CustomContextMenu);
    dialogMenu = new QMenu(this);
    QAction *action = new QAction(this);
    action->setText("Random number");
    connect(action, SIGNAL(triggered()), this, SLOT(slotRandomNumber()));
    dialogMenu->addAction(action);

    action = new QAction(this);
    action->setText("CalcArea");
    connect(action, SIGNAL(triggered()), this, SLOT(slotCalcArea()));
    dialogMenu->addAction(action);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButtonCalc_clicked()
{
    QString strRadius = ui->lineEditRadius->text();
    double radius = strRadius.toDouble();
    qDebug() << "Radius = " << radius;
    double area = M_PI * pow(radius, 2);
    QString strArea;
    strArea = QString::number(area, 'f', 6);
    //strArea.setNum(area, 'f', 8);
    //strArea.setNum(area);
    ui->lineEditArea->setText(strArea);
    counter++;
    QList<QString> rad_area;
    rad_area.append(QString::number(radius, 'f', 6));
    rad_area.append(QString::number(area, 'f', 6));
    list.insert(QString::number(counter), rad_area);
    stringList.append(QString::number(area, 'f', 3));

    if (list.size() == 2)
    {
        qDebug() << "\r";
        qDebug() << "Calc second time , print input & output info...";
        QVariant var(stringList);
        for (QMap<QString, QList<QString>>::iterator it = list.begin(); it != list.end(); it++)
        {
            qDebug() << "key = " << it.key() << " value = "<< it.value().at(0) << " ; " << it.value().at(1);
            qDebug() << "value = "<< (*it)[0] << " ; " << (*it)[1];
        }
        list.clear();

        qDebug() << "var type = " << var.type();
        QStringList strList = var.toStringList();
        for (int i = 0; i < strList.size(); i++)
        {
            qDebug() << "var_string_list = " << strList.at(i);
        }
        stringList.clear();
        qDebug() << "\r";
    }
}

void Dialog::on_Dialog_customContextMenuRequested(const QPoint &pos)
{
    (void)pos;
    dialogMenu->exec(QCursor::pos());
}

void Dialog::slotRandomNumber()
{
    double num = (double)(qrand() % 99) / 10.0;
    ui->lineEditRadius->setText(QString::number(num, 'f', 6));
}

void Dialog::slotCalcArea()
{
    //ui->pushButtonCalc->click();
    emit signalCalc();
}
